<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">

      <el-form-item label="属性名称" prop="attributeName">
        <el-input
          v-model="queryParams.attributeName"
          clearable
          placeholder="请输入属性名称"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">{{$t("search")}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t("reset")}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:attributePublic:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >{{$t("add")}}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-sort"
          plain
          size="mini"
          type="info"
          @click="toggleExpandAll"
        >{{$t("unfold")}}/{{$t("packUp")}}
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="attributeList"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      row-key="attributeId"
    >


      <el-table-column align="center" label="属性名称" prop="attributeName"/>
      <el-table-column align="center" label="显示顺序" prop="orderNum"/>


      <el-table-column align="center" class-name="small-padding fixed-width" :label="$t('controls')">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:attributePublic:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >{{$t("edit")}}
          </el-button>
          <el-button
            v-hasPermi="['system:attributePublic:add']"
            icon="el-icon-plus"
            size="mini"
            type="text"
            @click="handleAdd(scope.row)"
          >{{$t("add")}}
          </el-button>
          <el-button
            v-hasPermi="['system:attributePublic:remove']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >{{$t("delete")}}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改产品公有属性对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="上级属性" prop="parentId">
          <treeselect v-model="form.parentId" :normalizer="normalizer" :options="attributeOptions"
                      placeholder="请选择父部门id"/>
        </el-form-item>
        <el-form-item label="属性名称" prop="attributeName">
          <el-input v-model="form.attributeName" placeholder="请输入部门名称"/>
        </el-form-item>
        <el-form-item label="显示顺序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入显示顺序"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{$t("ok")}}</el-button>
        <el-button @click="cancel">{{$t("cancel")}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addAttribute, delAttribute, getAttribute, listAttribute, updateAttribute} from "@/api/system/attributePublic";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Attribute",
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 产品公有属性表格数据
      attributeList: [],
      // 产品公有属性树选项
      attributeOptions: [],


      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        parentId: null,
        ancestors: null,
        attributeName: null,
        orderNum: null,
        phone: null,
        email: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品公有属性列表 */
    getList() {
      this.loading = true;
      listAttribute(this.queryParams).then(response => {
        this.attributeList = this.handleTree(response.data, "attributeId", "parentId");
        this.loading = false;
      });
    },
    /** 转换产品公有属性数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.attributeId,
        label: node.attributeName,
        children: node.children
      };
    },
    /** 查询产品公有属性下拉树结构 */
    getTreeselect() {
      listAttribute().then(response => {
        this.attributeOptions = [];
        const data = {attributeId: 0, attributeName: '顶级节点', children: []};
        data.children = this.handleTree(response.data, "attributeId", "parentId");

        this.attributeOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        attributeId: null,
        parentId: null,
        ancestors: null,
        attributeName: null,
        orderNum: null,
        remark: null,
        phone: null,
        email: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.attributeId) {
        this.form.parentId = row.attributeId;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加产品公有属性";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.parentId;
      }
      getAttribute(row.attributeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品公有属性";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.attributeId != null) {
            updateAttribute(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('modifiedSuccessfully'));
              this.open = false;
              this.getList();
            });
          } else {
            addAttribute(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('newSuccess'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除产品公有属性编号为"' + row.attributeId + this.$t('dataItem')).then(function () {
        return delAttribute(row.attributeId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('successfullyDeleted'));
      }).catch(() => {
      });
    }
  }
};
</script>
